<?php

/*
 * Copyright 2015 jiaojie <jiaojie1989@gmail.com>.
 * 
 * Check your synatx if it is right.
 * Check your variables if the names are just correct.
 * Finally, run it under your command line.
 * Enjoy it!
 */

namespace Msign\Biz;

use Ziroom\Util\Redis\Singleton;

/**
 *
 * @author jiaojie <jiaojie1989@gmail.com>
 * @date 2015-4-16
 * @version 1.0.0
 * @description
 */
class PaymentLock {

    /**
     *
     * @var  Ziroom\Util\Redis\Client
     */
    protected $redis;

    public function __construct() {
        $this->redis = Singleton::getInstance();
    }

    public function getLockUid() {
        $response = array();
        $uids = $this->redis->keys('MSIGN::LOCK::UID::*');
        foreach ($uids as $k => $v) {
            $response[str_replace('MSIGN::LOCK::UID::', '', $v)] = $this->redis->ttl($v);
        }
        return $response;
    }

    public function getLockHouse() {
        $response = array();
        $houses = $this->redis->keys('MSIGN::LOCK::HOUSE::*');
        foreach ($houses as $k => $v) {
            $response[str_replace('MSIGN::LOCK::HOUSE::', '', $v)] = $this->redis->ttl($v);
        }
        return $response;
    }

    public function getLockContract() {
        $response = array();
        $contracts = $this->redis->keys('MSIGN::LOCK::CONTRACT::*');
        foreach ($contracts as $k => $v) {
            $response[str_replace('MSIGN::LOCK::CONTRACT::', '', $v)] = $this->redis->ttl($v);
        }
        return $response;
    }

    public function getUidPaymentSet() {
        $response = array();
        $uids = $this->redis->keys('MSIGN::SETS::UIDPAYMENT::*');
        foreach ($uids as $k => $v) {
            $response[str_replace('MSIGN::SETS::UIDPAYMENT::', '', $v)]['ttl'] = $this->redis->ttl($v);
            $response[str_replace('MSIGN::SETS::UIDPAYMENT::', '', $v)]['orders'] = $this->redis->sMembers($v);
        }
        foreach($response as $k => $v) {
            $response[$k]['count'] = count($v['orders']);
        }
        return $response;
    }

    public function getContractPaymentSet() {
        $response = array();
        $contracts = $this->redis->keys('MSIGN::SETS::CONTRACTPAYMENT::*');
        foreach ($contracts as $k => $v) {
            $response[str_replace('MSIGN::SETS::CONTRACTPAYMENT::', '', $v)]['ttl'] = $this->redis->ttl($v);
            $response[str_replace('MSIGN::SETS::CONTRACTPAYMENT::', '', $v)]['orders'] = $this->redis->sMembers($v);
        }
        foreach($response as $k => $v) {
            $response[$k]['count'] = count($v['orders']);
        }
        return $response;
    }
    
    public function unlockUid($uid = null) {
        $uid = trim($uid);
        if(empty($uid)) {
            return false;
        }
        if($this->redis->del('MSIGN::SETS::UIDPAYMENT::' . $uid)) {
            return $this->redis->del('MSIGN::LOCK::UID::' . $uid);
        }
        return false;
    }
    
    public function unlockContract($contract_code = null) {
        $contract_code = trim($contract_code);
        if(empty($contract_code)) {
            return false;
        }
        if($this->redis->del('MSIGN::SETS::CONTRACTPAYMENT::' . $contract_code)) {
            return $this->redis->del('MSIGN::LOCK::CONTRACT::' . $contract_code);
        }
        return false;
    }
    
    public function unlockHouse($house_code = null) {
        $contract_code = trim($contract_code);
        if(empty($contract_code)) {
            return false;
        }
        return $this->redis->del('MSIGN::LOCK::HOUSE::' . $house_code);
    }

}
